﻿using Common.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Wallpaper.Models.DTOs.Requests.Tag;
using Wallpaper.Models.DTOs.Responses.Tag;
using Wallpaper.Services.IService;

namespace WallpaperApi.Controllers
{
    [Route("api/tags")]
    [ApiController]
    [Authorize]
    public class TagsController : BaseController
    {

        readonly ITagService _tagService;
        readonly ILogger<TagsController> _logger;

        public TagsController(ITagService tagService, ILogger<TagsController> logger)
        {
            _tagService = tagService;
            _logger = logger;
        }


        /// <summary>
        /// 新增标签
        /// </summary>
        /// <param name="tagName"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResponse<Object>> Create([FromBody] TagRequest request)
        {
            await _tagService.CreateAsync(request.Name);
            return ApiResponse<Object>.Success();
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ApiResponse<Object>> Delete(string id)
        {
             await _tagService.DeleteAsync(id);
            return ApiResponse<Object>.Success();
        }


        /// <summary>
        /// 获取标签
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResponse<List<TagResponse>>> Get()
        {
          List<TagResponse> tagResponses=await _tagService.GetListAsync();
            return ApiResponse<List<TagResponse>>.Success(tagResponses);
        }


        /// <summary>
        /// 小程序获取热门标签
        /// </summary>
        /// <returns></returns>
        // 获取默认数量的热门标签
        [HttpGet("hot")]
        [AllowAnonymous]
        public async Task<ApiResponse<List<TagResponse>>> GetHotTags()
        {
            // 默认获取4条热门标签
            List<TagResponse> hotTags = await _tagService.GetHotTagsAsync(4);
            return ApiResponse<List<TagResponse>>.Success(hotTags);
        }

        /// <summary>
        /// 获取指定数量的热门标签
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        [HttpGet("{number}/hot")]
        [AllowAnonymous]
        public async Task<ApiResponse<List<TagResponse>>> GetHotTagsByNumber(int number)
        {
            // 根据传入数量获取热门标签
            number = Math.Max(1, Math.Min(number, 50)); // 限制范围1-50
            List<TagResponse> hotTags = await _tagService.GetHotTagsAsync(number);
            return ApiResponse<List<TagResponse>>.Success(hotTags);
        }
    }
}
